<?php
namespace AppHome\Controller;
use Think\Controller;
class TaskController extends Controller {
    /*
     * curl_post 父类Controller的curl方法
     * */
    /*流程任务提交功能首页*/
    public function index(){
        //$this->curl_post();
        //$result = M('task')->where()->find();
        $this->display();
    }
   
    /*司机任务API*/
    public function user_task(){
        $data = I('post.');//userid type=1待处理(待完成) type=2 已提交(完成) type=3未提交(执行中)
        /*负责*/
        $data['userid'] = session('userid');
        if(!$data['userid']){
           echo json_encode(array('status'=>0,'msg'=>'获取用户id失败'));
           die();
        }
        $where['userid'] = $data['userid'];
        $array = [];
        switch ($data['type']) {
        	case 1://status=0
        		$status['status'] = 0;
        		break;
        	case 2://status=1
        		$status['status'] = 1;
        		break;
        	case 3://status=2
        		$status['status'] = 2;
        		break;
        	default://未传type 
                break;
        }
        $taskids = M('part')->field('taskid')->where($where)->select();
        if($taskids){
            foreach ($taskids as $key => $value) {
        	    // 工单对应的详情数据
        	    if($status){
                    $result = M('task')->where(array('taskid'=>$value['taskid']))->where($status)->find();
        	    }else{
        	        $result = M('task')->where(array('taskid'=>$value['taskid']))->find();
        	    }
		        if($result){
                    $result['mainname'] = M('driver')->field('username')->where(array('userid'=>$result['userid']))->find()['username'];
				    $result['carnum'] = M('car')->field('carnum')->where(array('carid'=>$result['carid']))->find()['carnum'];
				    $result['cname'] = M('client')->field('cname')->where(array('clientid'=>$result['clientid']))->find()['cname'];
				    $array[$key] = $result;
		        }
		    }
	    }
        
       /*用户负责的任务单*/
       if($status){
       	   $res = M('task')->where($where)->where($status)->select();
       }else{
       	   $res = M('task')->where($where)->select();
       }
       if($res){
           foreach ($res as $key => $value) {
                $res[$key]['mainname'] = M('driver')->field('username')->where(array('userid'=>$value['userid']))->find()['username'];
			    $res[$key]['carnum'] = M('car')->field('carnum')->where(array('carid'=>$value['carid']))->find()['carnum'];
				$res[$key]['cname'] = M('client')->field('cname')->where(array('clientid'=>$value['clientid']))->find()['cname'];
           }
           //echo json_encode($res);
       }
       $total = array_merge($res,$array);
       if($total){
          echo json_encode(array('status'=>1, 'msg'=>$total));
       }else{
          echo json_encode(array('status'=>0, 'msg'=>'无任务单'));
       }

       
    }

   /*任务单状态更新API*/ 
 //code=1:待处理=>执行中 2:完成;taskid
    public function task_update(){
	     $data = I('post.');
	     if(!$data['taskid']){
	        echo json_encode(array('status'=>0, 'msg'=>'未获取任务id'));
	        die();
	     }else if(!$data['code']){
	        echo json_encode(array('status'=>0, 'msg'=>'未获取状态码'));
	        die();
	     }
	     $m = M('task');
	     $status = $m->where(array('taskid'=>$data['taskid']))->getField('status');
	     switch ($data['code']) {
	     	case 1:
	     	//由待处理=>执行中
	     		if($status == 2){
	               echo json_encode(array('status'=>0, 'msg'=>'当前状态已经是任务进行中,请勿重新操作'));
	               die();
	     		}else if($status == 1){
	               echo json_encode(array('status'=>0, 'msg'=>'该任务已完成'));
	               die();
	     		}
	     		$statu['status'] = 2;
	            break;
	     	case 2:
	     	//由执行中=>完成
	     		if($status == 0){
	               echo json_encode(array('status'=>0, 'msg'=>'该任务未处理'));
	               die();
	     		}else if($status == 1){
	               echo json_encode(array('status'=>0, 'msg'=>'该任务已完成'));
	               die();
	     		}
	     		$statu['status'] = 1;
	            break;
	        default:
	               echo json_encode(array('status'=>0, 'msg'=>'状态码不合法'));
	               die();
                break;
	     }
	     $res = $m->where(array('taskid'=>$data['taskid']))->save($statu);
	     if(!$res){
	        echo json_encode(array('status'=>0, 'msg'=>'状态更新失败'));
	     }else{
	        echo json_encode(array('status'=>1, 'msg'=>'状态更新成功'));
	     }

    }


/*上传GPS信息API*/


}